// pages/category/category.js
var common = require("../../utils/Conmon.js");

var shopID = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CategoryStatus: 0,
    CategoryList: {},
    SubCategoryList: {},
    TitleClass: '',
    TitleURL: '',
    TitleName: '',
    shopID:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit( function () {
    });
    if (options.shopID > 0) {
      shopID = options.shopID;
      this.setData({
        shopID: shopID
      })
    }
    GetProductCategory(this, 0);  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 获取商品二三级分类
   */
  onGetProductCategory: function (event) {
    var catalogId = event.currentTarget.id;
    var catalogName = event.currentTarget.dataset.name;
    this.setData({
      TitleClass: 'golist-btn',
      TitleURL: '/pages/goodsList/goodsList?CatalogId=' + catalogId + "&shopID=" + shopID,
      TitleName: '查看' + catalogName + '类商品列表'
    })
    GetProductCategory(this, catalogId);
  },

  /**
   * 跳转到商品列表
   */
  onToGoodsList: function (event) {
    wx.redirectTo({
      url: '/pages/goodsList/goodsList?CatalogId=' + event.currentTarget.id + "&shopID=" + shopID
    })
  }
})

//获取商品分类
function GetProductCategory (obj, catalogId) {
  wx.showLoading({
    title: '加载中...',
    mask: true
  });

  var that = obj;
  var globalData = getApp().globalData;
  var custId = globalData.custID;
  var url = globalData.apiurl + '/mobile/api/Product/GetProductCategory';

  wx.request({
    url: url,
    data: { "custID": custId, "catalogId": catalogId, "status": that.data.CategoryStatus, "shopID": shopID },
    method: 'GET',
    header: {
      'content-type': 'application/json',
      'X_ManuName': globalData.manuName
    },
    success: function (res) {
      var list = res.data.content;
      if (catalogId == 0) {
        that.setData({
          CategoryList: list
        })
      } else {
        // var arr = new Array();
        // for (var i = 0; i < list.length; i++) {
        //   var data = new Object();
        //   data.Name = list[i].Name;
        //   GetProductList(list[i].CatalogID).then(function (response) {
        //     data.ProductList = response;
        //     arr.push(data);                        
        //   }).then(function () {
        //     that.setData({
        //       SubCategoryList: arr
        //     })
        //   });
        // }
            that.setData({
              SubCategoryList: list
            })
      }
      wx.hideLoading();      
    },
    fail: function () {
      wx.hideLoading();
      wx.showToast({
        title: '获取商品分类失败',
        duration: 2000
      })
    },
    complete: function () {
      //complete
    }
  })
}

//获取商品列表
function GetProductList (id) {
  return new Promise(function (resolve, reject) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/mobile/api/Product/GetProductList';

    wx.request({
      url: url,
      data: { ProductCatalog: id, CustID: custId, Status: 1 },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'X_ManuName': globalData.manuName
      },
      success: function (res) {
        resolve(res.data.content);
      },
      fail: function () {
        wx.showToast({
          title: '获取商品列表失败',
          duration: 2000
        })
      },
      complete: function () {
        //complete
      }
    })
  });
}